#!/usr/bin/env bash
# Usage: sbtenv completions <command> [arg1 arg2...]

set -e
test -n "${SBTENV_DEBUG}" && set -x

COMMAND="${1}"
if [ -z "${COMMAND}" ]; then
  sbtenv-help --usage completions >& 2
  exit 1
fi

COMMAND_PATH="$(command -v "sbtenv-${COMMAND}" || command -v "sbtenv-sh-${COMMAND}")"
if grep -iE "^([#%]|--|//) provide sbtenv completions" "${COMMAND_PATH}" > /dev/null; then
  shift
  exec "${COMMAND_PATH}" --complete "${@}"
fi
